home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DIRS.SWG / 0003_Search All Dirs & Subs #2.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  86 lines

  1. AH>>Hi everyone.  I have a small problem.  How does one go about accessing
  2.   >>EVERY File in every directory, sub-directory on a drive? I guess this is
  3.   >>part of the last question, but how do you access every sub-directory?
  4.  
  5. Unit FindFile;
  6. {$R-}
  7. Interface
  8.  
  9. Uses Dos;
  10.  
  11. Type
  12.   FileProc = Procedure ( x : PathStr );
  13.  
  14. Procedure FindFiles (DirPath : PathStr;      (* initial path           *)
  15.                      Mask : String;          (* mask to look For       *)
  16.                      Recurse : Boolean;      (* recurse into sub-dirs? *)
  17.                      FileDoer : FileProc);   (* what to do With found  *)
  18.  
  19. (* Starting at <DirPath>, FindFiles will pass the path of all the Files
  20.    it finds that match <Mask> to the <FileDoer> Procedure.  if <Recurse>
  21.    is True, all such Files in subdirectories beneath <DirPath> will be
  22.    visited as well.  if <Recurse> is False, the names of subdirectories
  23.    in <DirPath> will be passed as well. *)
  24.  
  25. Implementation
  26.  
  27. Procedure FindFiles (DirPath : PathStr;      (* initial path           *)
  28.                      Mask : String;          (* mask to look For       *)
  29.                      Recurse : Boolean;      (* recurse into sub-dirs? *)
  30.                      FileDoer : FileProc);   (* what to do With found  *)
  31.  
  32.   Procedure SubVisit ( DirPath : PathStr );
  33.   Var
  34.     Looking4 : SearchRec;
  35.  
  36.   begin
  37.     FindFirst ( Concat ( DirPath, Mask ), AnyFile, looking4);
  38.     While ( DosError = 0 ) Do begin
  39.       if ( looking4.attr and ( VolumeID + Directory ) ) = 0
  40.        then FileDoer ( Concat ( DirPath, looking4.name ) );
  41.       FindNext ( Looking4 );
  42.       end;   (* While *)
  43.     if Recurse
  44.      then begin
  45.       FindFirst ( Concat ( DirPath, '*.*' ), AnyFile, looking4);
  46.       While ( DosError = 0 ) and ( looking4.name [1] = '.' ) Do
  47.         FindNext (looking4);   (* skip . and .. directories *)
  48.       While ( DosError = 0 ) Do begin
  49.         if ( ( looking4.attr and Directory ) = Directory )
  50.          then SubVisit ( Concat ( DirPath, looking4.name, '\' ) );
  51.         FindNext ( Looking4 );
  52.         end;   (* While *)
  53.       end;   (* if recursing *)
  54.   end;   (* SubVisit *)
  55.  
  56.  
  57. begin   (* FindFiles *)
  58.   SubVisit ( DirPath );
  59. end;   (* FindFiles *)
  60.  
  61. end.
  62.  
  63.    --------------------------------------------------------------------
  64.  
  65. Program Visit;
  66.  
  67. Uses Dos, FindFile;
  68.  
  69. {$F+}
  70. Procedure FoundOne ( Path : PathStr );  (* MUST be Compiled With $F+ *)
  71. {$F-}
  72. begin
  73.   WriteLn ( Path );
  74. end;
  75.  
  76. begin
  77.   WriteLn ( '-------------------------------------------------------------');
  78.   FindFiles ( '\', '*.*', True, FoundOne );
  79.   WriteLn ( '-------------------------------------------------------------');
  80. end.
  81.  
  82.    -----------------------------------------------------------------------
  83.  
  84. FoundOne will be passed every File & subdirectory.  if you just want the
  85. subdirectories, ignore any name that doesn't end in a '\' Character!
  86.